import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

import config
from view import login, get_view_data, ding_upload
from tools import web_find_element
from ding_api import get_access_token


if __name__ == '__main__':

    s = Service(executable_path='chromedriver')

    # 获取钉钉的access_token
    token = {}
    ding_access_toke = get_access_token()
    token['access_token'] = ding_access_toke.get('access_token') or None
    token['expires'] = time.time() * 1000 + ding_access_toke.get('expires_in') - 60 or None
       
    for equipment in config.EQUIPMENT_LIST:
        bro = webdriver.Chrome(service=s)
        # 浏览器登录web端
        login(bro, equipment['url'])
        time.sleep(5)

        # 点击【事件查询】按钮
        event = web_find_element(bro, '//*[@id="nav"]/li[3]/a')
        event.click()

        time.sleep(15)

        # 循环人员名单
        for user_number,value in config.user_dict.items():
            user_number_input = web_find_element(bro, '//*[@id="main"]/div[2]/div/div[1]/div/div[1]/div[2]/input')
            user_number_input.clear()
            user_number_input.send_keys(user_number)
            search_button = web_find_element(bro, '//*[@id="main"]/div[2]/div/div[1]/div/button')
            search_button.click()  

            # 递归循环点击下一页，将页面数据写入rel_data
            rel_data = []
            get_view_data(bro, rel_data, equipment) 
            # 将查询出的结果，调用钉钉API，上传
            rel = ding_upload(token, rel_data)

        bro.quit()